﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Contra
{
    class Enemy
    {
        #region fields
        protected Texture2D texture;       
        protected Vector2 position;
        protected Vector2 velocity;
        protected Texture2D bulletTexture;
        protected float shoot = 0;


        public List<Bullet> bullets = new List<Bullet>();
    


        public Rectangle BulletRec
        {
            get;
            set;
        }


        public Rectangle enemyRec
        {
            get;
            set;
        }

        public bool IsVisible
        {
            get;
            set;
        }

        public int Health
        {
            get;
            set;
        }
        public bool IsAlive
        {
            get
            {
                return Health > 0;
            }
        }

        public void GotHit()
        {
            if (Health > 0)
            {
                Health--;
            }
        }
        #endregion fields
        public Enemy(Texture2D newTexture, Vector2 newPosition, Texture2D bTexture)
        {
            texture = newTexture;
            position = newPosition;
            bulletTexture = bTexture;        

            velocity = new Vector2(-1, -1);
        }

        public virtual void UpDate(GraphicsDevice graphics, GameTime gameTime)
         {
            position += velocity;
            if(position.Y <= 0 || position.Y >= graphics.Viewport.Height - texture.Height)
            {
                velocity.Y = -velocity.Y;
            }

            if(position.X < 0 - texture.Width)
            {
                IsVisible = false;
            }
            enemyRec = new Rectangle((int)position.X, (int)position.Y, 31, 50);

            shoot += (float)gameTime.ElapsedGameTime.TotalSeconds;
            if(shoot > 1)
            {
                shoot = 0;
                ShootBullets();
            }
            UpdateBullets();
            BulletRec = new Rectangle((int)position.X, (int)position.Y, 10, 10);


        }

        public void UpdateBullets()
        {
            foreach (var bullet in bullets)
            {
                bullet.position += bullet.velocity;
                if (bullet.position.X < 0)
                {
                    bullet.isVisible = false;
                }
            }
            for (int i = 0; i < bullets.Count; i++)
            {
                if (!bullets[i].isVisible)
                {
                    bullets.RemoveAt(i);
                    i--;
                }
            }
        }

        public Bullet ShootSingleBullet(float startX, float startY)
        {       
            Bullet newBullet = new Bullet(bulletTexture);
            newBullet.velocity.X = velocity.X - 3f;
            newBullet.position = new Vector2(startX, startY);
            newBullet.isVisible = true;
            return newBullet;
        }

        public virtual void ShootBullets()
        {
            Bullet newBullet = ShootSingleBullet(position.X + velocity.X - 3f, position.Y + (bulletTexture.Height / 2) - (bulletTexture.Width / 2));
         
            if (bullets.Count < 3)
            {
                bullets.Add(newBullet);
            }
        }

        public void LoadContent(ContentManager content)
        {
            bulletTexture = content.Load<Texture2D>("Bullet");
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            foreach (var bullet in bullets)
            {
                bullet.Draw(spriteBatch);
            }
            spriteBatch.Draw(texture, position , Color.White);
        }

      
    }
}
